package com.ncell.cell;

import java.util.ArrayList;
import java.util.List;

public class I extends AbstractI {
    private boolean permanent;
    private TemporalExtension currentBestTemporalExtension = null;
    private List<TemporalExtension> temporalExtensionList = null;

    public I() {
    }

    public I(boolean isPermanent) {
        this.permanent = isPermanent;
        if (isPermanent) {
            temporalExtensionList = new ArrayList<>();
        }
    }

    public List<TemporalExtension> getTemporalExtensionList() {
        if (!isPermanentI()) {
            throw new IllegalStateException("TP does not have temporal extensions");
        }
        return temporalExtensionList;
    }

    public int getWorstTemporalExtensionIndex() {
        if (temporalExtensionList.isEmpty()) {
            return -1;
        }

        float worstActivationCounter = temporalExtensionList.get(0).getActivationCounter();
        int worstIndex = -1;
        for (int i = 0; i < temporalExtensionList.size(); i++) {
            TemporalExtension ext = temporalExtensionList.get(i);
            if (ext.getActivationCounter() < worstActivationCounter) {
                worstActivationCounter = ext.getActivationCounter();
                worstIndex = i;
            }
        }

        return worstIndex;
    }

    public TemporalExtension getCurrentBestTemporalExtension() {
        return currentBestTemporalExtension;
    }

    public void setCurrentBestTemporalExtension(TemporalExtension currentBestTemporalExtension) {
        this.currentBestTemporalExtension = currentBestTemporalExtension;
    }

    @Override
    public boolean isPermanentI() {
        return permanent;
    }

    public void convertToPermanent() {
        permanent = true;
        temporalExtensionList = new ArrayList<>();
    }

    public void setAllPsChangeStateToConnectedToActive() {
        for (PConnection c : getConnections()) {
            c.getP().setConnectedToActive(true);
        }
    }

    public void clear() {
        setActivateCounter(0);
        setOarkn(0);
        setActive(false);
        getConnections().clear();
    }
}
